package com.fiidee.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.fiidee.blog.common.ERRORCODE;
import com.fiidee.blog.entity.Blog;
import com.fiidee.blog.entity.BlogData;
import com.fiidee.blog.service.BlogService;
import com.fiidee.blog.util.ReturnJsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by 瑱彬 on 2015/5/27.
 */
@Controller
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    BlogService blogService;

    @RequestMapping("/publish")
    @ResponseBody
    public JSONObject doPublish(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                                @CookieValue(value = "username", required = true, defaultValue = "") String username,
                                HttpServletRequest request, HttpServletResponse response) {
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        if (0 == id || username == null) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        Blog blog = new Blog();
        BlogData blogData = new BlogData();

        blog.setTitle(request.getParameter("title"));
        blog.setKword(request.getParameter("kword"));
        blog.setOriginal(Integer.valueOf(request.getParameter("original")));
        blog.setCatid(Integer.valueOf(request.getParameter("catid")));

        blog.setSource(request.getParameter("source"));
        blog.setTags(request.getParameter("tags"));
        blog.setAuthor(username);
        blog.setUserid(id);

        blogData.setContent(request.getParameter("content"));

        int code = blogService.doPublish(blog, blogData, response);
        return ReturnJsonUtil.returnMsg(code);
    }

    @RequestMapping("/autosave")
    @ResponseBody
    public JSONObject doSave(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                             @CookieValue(value = "username", required = true, defaultValue = "") String username,
                             HttpServletResponse response) {
        return null;
    }

    @RequestMapping("/list")
    @ResponseBody
    public JSONObject getHomeList(@RequestParam(value = "page",required = false,defaultValue = "0") String page){
        List<Map<String,Object>> blogs = blogService.getHomeList(Integer.valueOf(page));
        return ReturnJsonUtil.successRows(blogs);
    }

    @RequestMapping("/top")
    @ResponseBody
    public JSONObject topBlog(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                              int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.topBlog(id, blogid));
    }

    @RequestMapping("/canceltop")
    @ResponseBody
    public JSONObject cancelTopBlog(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                              int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.cancelTopBlog(id, blogid));
    }

    @RequestMapping("/trash")
    @ResponseBody
    public JSONObject trashBlog(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                              int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.trashBlog(id, blogid));
    }


    @RequestMapping("/recovery")
    @ResponseBody
    public JSONObject recoveryBlog(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                                int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.recoveryBlog(id, blogid));
    }


    @RequestMapping("/collection")
    @ResponseBody
    public JSONObject collectionBlog(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                              int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.collectionBlog(id, blogid));
    }

    @RequestMapping("/cancelcollection")
    @ResponseBody
    public JSONObject cancelCollecttion(@CookieValue(value = "userId", required = true, defaultValue = "") String userId,
                                    int blogid){
        int id;
        try {
            id = Integer.valueOf(userId);
        } catch (NumberFormatException e) {
            return ReturnJsonUtil.returnMsg(ERRORCODE.NOLOGIN);
        }
        return ReturnJsonUtil.returnMsg(blogService.cancelCollecttion(id, blogid));
    }

}
